package switchviz.recording;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JPanel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.log4j.Logger;
import org.jdom.JDOMException;

import switchviz.core.ResourceManager;
import switchviz.gui.component.PopupMenu;
import switchviz.util.FileOperations;

public class RecordPanel extends JPanel implements TreeSelectionListener, MouseListener, ActionListener, ComponentListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 747732245873052572L;
	private static Logger logger = Logger.getLogger(RecordPanel.class);
	private RecordTree _recordtree = null;
	private RecordConfigurationPanel _recordconf = null;
	private PopupMenu _popup;
	
	public RecordPanel()
	{
		try
		{
			if (logger.isInfoEnabled()) { logger.info("Initialize ..."); }
			initialize();
		}
		catch(Exception e)
		{
			logger.error(e);
		}
	}
	
	public RecordTree getRecordTree() throws JDOMException, IOException
	{
		if(_recordtree == null)
		{
			_recordtree = new RecordTree();
			_recordtree.getTree().addTreeSelectionListener(this);
			_recordtree.getTree().addMouseListener(this);
			_popup = new PopupMenu(new String[] {ResourceManager.getInstance().getActionRemoveRecord(), 
					ResourceManager.getInstance().getActionRefresh()}, this);
		}
		return _recordtree;
	}
	
	public RecordConfigurationPanel getRecordConfiguration()
	{
		if(_recordconf == null)
		{
			_recordconf = new RecordConfigurationPanel();
		}
		return _recordconf;
	}
	
	public void reload()
	{
		try
		{
			getRecordTree().reload();
		}
		catch(Exception e)
		{
			logger.error(e);
		}
	}
	
	private void initialize() throws JDOMException, IOException
	{
		this.setLayout(ResourceManager.getInstance().getBorderLayout());
		this.add(getRecordConfiguration(), BorderLayout.CENTER);
		this.add(getRecordTree(), BorderLayout.WEST);
		this.addComponentListener(this);
	}

	public void valueChanged(TreeSelectionEvent evt) 
	{
		DefaultMutableTreeNode mtn = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
		RecordDataModel rdm = null;
		try
		{
			if(mtn.getUserObject().getClass().equals(RecordDataModel.class))
			{
				rdm = (RecordDataModel)mtn.getUserObject();
			}
			else if(mtn.getUserObject().getClass().equals(String.class))
			{
				
				rdm = (RecordDataModel)((DefaultMutableTreeNode)mtn.getParent()).getUserObject();
				Cursor cursor = this.getCursor();
				this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				getRecordConfiguration().setSignal(new File(rdm.getFolder(), mtn.getUserObject().toString()));
				this.setCursor(cursor);
			}
			if(rdm != null)
				getRecordConfiguration().setReplayPanel(rdm);
		}
		catch(Exception e)
		{
			logger.error(e);
		}
	}

	public void mousePressed(MouseEvent e) 
	{
		if (e.isPopupTrigger() || e.getButton() == 3) 
			_popup.show(e.getComponent(), e.getX(), e.getY());
	}
	
	public void actionPerformed(ActionEvent evt) 
	{
		String command = evt.getActionCommand();
		if(ResourceManager.getInstance().getActionRemoveRecord().equals(command))
		{
			try
			{
				DefaultMutableTreeNode mtn = (DefaultMutableTreeNode) getRecordTree().getTree().getLastSelectedPathComponent();
				File file = null;
				
				if(mtn.getUserObject().getClass().equals(RecordDataModel.class))
				{
					file = ((RecordDataModel)mtn.getUserObject()).getFolder();
				}
				else if(mtn.getUserObject().getClass().equals(String.class))
				{
					RecordDataModel rdm = (RecordDataModel)((DefaultMutableTreeNode)mtn.getParent()).getUserObject();
					file = new File(rdm.getFolder(), mtn.getUserObject().toString());
				}
				if(!FileOperations.deleteAll(file))
					logger.error("Could not delete Record in File " + file.getAbsolutePath());
				else
				{
					if (logger.isInfoEnabled()) 
					{ 
						logger.info("Record in File " + file.getAbsolutePath() + " is deleted."); 
					}
				}
				reload();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		else if(ResourceManager.getInstance().getActionRefresh().equals(command))
		{
			reload();
		}
	}
	
	public void componentShown(ComponentEvent evt) 
	{
		reload();
	}
	
	public void componentHidden(ComponentEvent arg0) {}
	public void componentMoved(ComponentEvent arg0) {}
	public void componentResized(ComponentEvent arg0) {}
	
	public void mouseClicked(MouseEvent arg0) {}
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {}
}
